package org.android.kumoh_ing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONObject;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class SettingActivity extends Activity {
	private RadioGroup rg;
	private RadioButton rg1, rg2;
	private Button btnNum;
	private EditText editNum;
	LocationDBTransfer transfer;
//	FileInputStream fis;
	//String myNum,myEmail;
	String Info;
	String MY_id;
	String resStr;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		transfer = new LocationDBTransfer();
		rg = (RadioGroup) findViewById(R.id.radioGroup);
		rg1 = (RadioButton) findViewById(R.id.radio1);
		rg2 = (RadioButton) findViewById(R.id.radio2);
		btnNum = (Button) findViewById(R.id.btnNum);
		editNum = (EditText) findViewById(R.id.editNum);
		MY_id = getIntent().getExtras().getString("my_id");
		Info ="#%UserInfor="
				+ Kumoh_ingActivity.getAppPreferences(this, "facebook_myid") + "$"
				+ Kumoh_ingActivity.My_phone+"$"
				+ Kumoh_ingActivity.My_gmail+ "//";
				
		try {
			if(transfer.getPermission(MY_id).equalsIgnoreCase("o")){
				rg1.setChecked(true);
			}
			else if(transfer.getPermission(MY_id).equalsIgnoreCase("x")){
				rg2.setChecked(true);
			}
		
			rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
				@Override
				public void onCheckedChanged(RadioGroup group, int id) {
					// TODO Auto-generated method stub
					try {
						if(group==rg){
							if(id==R.id.radio1){
								if(transfer.getLocation(MY_id).equalsIgnoreCase(" "))
									Toast.makeText(getApplicationContext(), "GPS를 켜주세요.", 1000).show();
								transfer.setPermission(MY_id, "o");
							}
							else if(id==R.id.radio2){
								transfer.setPermission(MY_id, "x");
							}
						}
						}catch (ClientProtocolException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
		}catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		btnNum.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				String myNum = editNum.getText().toString();
			
				if(myNum.length()<1){
					Toast.makeText(getApplicationContext(), "입력 하세요.", Toast.LENGTH_LONG).show();
				}
				else{
					try {
						Bundle params = new Bundle();
						Bundle params1 = new Bundle();
						params.putString("fields", "privacy");		
						String responseStr = facebookvalue.FacebookInstance.request(
								"me/groups", params);			
						params1.putString("message", Info);
						
						JSONObject resultObj = Util.parseJson(responseStr);
						JSONArray jArray = resultObj.getJSONArray("data");
						
					    for (int i = 0; i < jArray.length(); i++) {
							JSONObject obj = jArray.getJSONObject(i);
							String curPrivacy = obj.getString("privacy");
							if (curPrivacy.equalsIgnoreCase("SECRET") == false)
								continue;
							String group_id = obj.getString("id");	
							facebookvalue.FacebookInstance.request(group_id+"/feed", params1,"POST");
					    }
						

						//mInText.setText("");
						
					} catch (Exception ex) {
						ex.printStackTrace();
					} catch (FacebookError e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Toast.makeText(getApplicationContext(), "저장", 1000).show();
				}
			}

		});
		
		
	}
	
	
}
